package com.ytkj.digitalConstruction.service.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ytkj.digitalConstruction.dao.permission.FlowMapper;
import com.ytkj.digitalConstruction.dao.permission.FlowOperationMapper;
import com.ytkj.digitalConstruction.dao.permission.OperationMapper;
import com.ytkj.digitalConstruction.entity.permission.Flow;
import com.ytkj.digitalConstruction.entity.permission.FlowOperation;
import com.ytkj.digitalConstruction.entity.permission.Operation;
import com.ytkj.digitalConstruction.httpbean.permission.ResFlow;
import com.ytkj.digitalConstruction.httpbean.permission.ResOperation;

/**
 * 流程模板业务处理类
 * 
 * @author Pete
 * @Date 2017年12月14日
 */
@Service
public class FlowService {

	@Autowired
	private FlowMapper flowMapper;

	@Autowired
	private OperationMapper operationMapper;

	@Autowired
	private FlowOperationMapper flowOperationMapper;

	/** 流程根节点 */
	private final static int ROOT_FLOW = 0;

	/**
	 * 获取全部菜单操作
	 * 
	 * @return
	 */
	public List<ResFlow> getFullFlowsAndOperations() {
		return getFlowAndOperation(ROOT_FLOW);
	}

	/**
	 * 获取菜单操作树
	 * 
	 * @param parentId
	 * @return
	 */
	public List<ResFlow> getFlowAndOperation(int parentId) {
		List<ResFlow> list = new ArrayList<>();
		List<Flow> flows = flowMapper.selectByParentId(parentId);
		for (Flow m : flows) {
			ResFlow rm = new ResFlow(m);
			List<FlowOperation> fos = flowOperationMapper.selectByFlowId(m.getFlowId());
			if (fos != null) {
				for (FlowOperation fo : fos) {
					Operation o = operationMapper.selectByPrimaryKey(fo.getOperationId());
					ResOperation ro = new ResOperation(o);
					ro.setHave(false);
					rm.setOperation(ro);
				}
			}
			rm.setFlows(getFlowAndOperation(m.getFlowId()));
		}
		return list;
	}

	/**
	 * 根据关联ID查询流程操作
	 * 
	 * @param id
	 * @return
	 */
	public Map<Integer, Map<String, String>> getFlowOperation(int id) {
		Map<Integer, Map<String, String>> map = new HashMap<>();
		FlowOperation flowOperation = flowOperationMapper.selectByPrimaryKey(id);
		Flow flow = flowMapper.selectByPrimaryKey(flowOperation.getFlowId());
		Operation operation = operationMapper.selectByPrimaryKey(flowOperation.getOperationId());
		Map<String, String> ops = new HashMap<>();
		ops.put("id", operation.getOperationId() + "");
		ops.put("name", operation.getOperationName());
		ops.put("url", operation.getOperationUrl());
		ops.put("desc", operation.getOperationDesc());
		ops.put("have", "false");
		map.put(flow.getFlowId(), ops);
		return map;
	}

	/**
	 * 根据关联ID查询流程操作
	 * 
	 * @param id
	 * @return
	 */
	public ResFlow getResFlowById(int id) {
		FlowOperation flowOperation = flowOperationMapper.selectByPrimaryKey(id);
		if (flowOperation != null) {
			Flow flow = flowMapper.selectByPrimaryKey(flowOperation.getFlowId());
			Operation operation = operationMapper.selectByPrimaryKey(flowOperation.getOperationId());
			if (flow != null && operation != null) {
				ResOperation resOperation = new ResOperation(operation);
				ResFlow resFlow = new ResFlow(flow);
				resFlow.setOperation(resOperation);
				return resFlow;
			}
		}
		return null;
	}

}
